import { getLodop } from "@/utils/LodopFuncs"
import { parseTime4 } from "@/utils"
var LODOP, P_ID = "", TaskID1, TaskID2, t, c = 0;//声明为全局变量 
export function checkPrinter() {
  let LODOP = getLodop();
  if (!LODOP) return false;
  return true;
}
export function print(data) {
  if (Array.isArray(data)) {
    for (let i = 0; i < data.length; i++) {
      printSignal(data[i]);
    }
  } else {
    printSignal(data);
  }

}
function printSignal(data) {
  LODOP = getLodop();
  if (!LODOP) return;
  LODOP.SET_LICENSES("", "EE0887D00FCC7D29375A695F728489A6", "C94CEE276DB2187AE6B65D56B3FC2848", "");
  // LODOP.SET_LICENSES("","13F0BE65846276CB60111433C6280000","","");
  LODOP.PRINT_INITA("0mm", "0mm", "210mm", "148mm", "裕酷科技_ERP系统_拣货单_拣货单打印");
  let { chooseNumber, commodityNum, regionName,groupName } = data;
  LODOP.SET_PRINT_PAGESIZE(2, 0, 0, "A5");
  LODOP.ADD_PRINT_TABLE(112, 0, "210mm", "700px", getTableStr(data));
  LODOP.ADD_PRINT_HTM(25, "80.869%", 146, 28, `<p style="font-weight:bold;font-size:16px"><span tdata='pageNO'>第##页</span> <span tdata='pageCount'>共##页</span></p>`);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
  LODOP.SET_PRINT_STYLEA(0, "Horient", 1);
  LODOP.ADD_PRINT_HTM(25, 40, 300, 34, `<p style="font-weight:bold;font-size:16px;">区域:${regionName}</p>`);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
  LODOP.ADD_PRINT_HTM(25, "45.00%", 113, 35, `<p style="font-weight:bold;font-size:16px">品规数:${commodityNum}</p>`);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
  LODOP.SET_PRINT_STYLEA(0, "Horient", 4);
  LODOP.ADD_PRINT_HTM(70, 42, 250, 40, `<p style="font-weight:bold;font-size:18px">单号:${chooseNumber}</p>`);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
  LODOP.ADD_PRINT_HTM(70, "45.00%", 250, 40, `<p style="font-weight:bold;font-size:16px">通道组:${groupName}</p>`);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
  LODOP.PRINT();
  // LODOP.PRINT_DESIGN();

  // LODOP.SET_PRINT_MODE("CATCH_PRINT_STATUS", true)
  // if (LODOP.CVERSION) {
  //   LODOP.On_Return = function (TaskID, Value) {
  //     P_ID = Value;
  //     console.log("该任务走的是c-lodop方式，job代码为" + P_ID);
  //     if (P_ID != "") { c = 0; C_WaitFor(); }else { console.log("已取消打印");};
  //   };
  //   LODOP.PRINT();
  // } else {
  //   P_ID = LODOP.PRINT();
  //   console.log("该任务走的是lodop插件方式，job代码为" + P_ID);
  //   if (P_ID != "") { c = 0; WaitFor(); } else { console.log("已取消打印");};
  // };
}
// 拣货单
export function getTableStr(data) {
  let { chooseDetailsVoList } = data;
  console.log(chooseDetailsVoList);
  let style = `<style>
table {border-collapse: collapse;width: 700px !important;margin: 0 auto;}
td,th {text-align: center;border-bottom: 1px solid #000;padding: 1px;padding:10px 0 5px;}
td{font-size:14px}
</style><body>`;
  let tableStr = "<table><thead><tr><th>序号</th><th>货位号</th><th>商品名称</th><th>规格</th><th>单位</th><th>数量</th></tr></thead><tbody>" +
    chooseDetailsVoList.map((row, index) => {
      return `<tr>
            <td>${index + 1}</td>
            <td style="font-weight:bold">${row.positionName}</td>
            <td>${row.commodityName}</td>
            <td>${row.standard}</td>
            <td>${row.unitName}</td>
            <td>${row.procureNumber}</td>
          </tr>`
    }).join("") + "</tbody></table>" + "</body>";
  return style + tableStr;
}











// 打印随货单
export function printFollow(data) {
  LODOP = getLodop();
  if (!LODOP) return;
  LODOP.SET_LICENSES("", "EE0887D00FCC7D29375A695F728489A6", "C94CEE276DB2187AE6B65D56B3FC2848", "");
  // LODOP.SET_LICENSES("","13F0BE65846276CB60111433C6280000","","");
  LODOP.PRINT_INITA("0mm", "0mm", "210mm", "148mm", "裕酷科技_ERP系统_随货单打印");
  LODOP.SET_PRINT_PAGESIZE(2, 0, 0, "A5");
  LODOP.ADD_PRINT_TABLE(112, 0, "210mm", "320px", getFollowTableStr(data));
  LODOP.SET_PRINT_STYLEA(0, "ALIGNMENT", 2);
  LODOP.ADD_PRINT_HTM(25, "80.869%", 146, 28, `<p style="font-weight:bold;font-size:16px"><span tdata='pageNO'>第##页</span> <span tdata='pageCount'>共##页</span></p>`);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
  LODOP.ADD_PRINT_HTM(25, 0, "210mm", 30, `<h1 style="text-align:center;font-family:'楷体';margin:0 auto;font-size:26px">裕酷科技统一配送单</h1>`);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
  // 一维码
  LODOP.ADD_PRINT_BARCODE(10, "6%", 220, 75,"128Auto",data.followNumber);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
  LODOP.ADD_PRINT_HTM(88, 50, "100mm", 25, `<p style="font-size:16px">配送单号：${data.followNumber}</p>`);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
  LODOP.ADD_PRINT_HTM(88, "74%", "100mm", 25, `<p style="font-size:16px">出货日期：${parseTime4(data.printTime)}</p>`);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
  LODOP.ADD_PRINT_HTM(490, 45, "100mm", 25, `<p style="font-size:16px">出货人：</p>`);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
  LODOP.ADD_PRINT_HTM(490, 245, "100mm", 25, `<p style="font-size:16px">送货人：</p>`);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
  LODOP.ADD_PRINT_HTM(490, 445, "100mm", 25, `<p style="font-size:16px">接货人：</p>`);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
  LODOP.PRINT();
  // LODOP.PRINT_DESIGN();
}

// 随货单
export function getFollowTableStr(data) {
  let { storeFollowList } = data;
  let style = `<style>
.print-tab {border-collapse: collapse;width: 700px !important;margin: 0 auto;}
.print-tab td,.print-tab th {text-align: center;border: 1px solid #000;padding: 3px;}
.print-tab td{font-size:14px}
</style><body>`;
  let tableStr = "<table class='print-tab'>" +
    `<thead><tr><td>出货方</td><td colspan="3" style="text-align:left !important;">${data.warehouseName}</td><td>收货方</td><td colspan="5"  style="text-align:left !important;">${data.storeName}</td></tr>` +
    "<tr><td>商品编码</td><td>条形码</td><td>商品名称</td><td>规格</td><td style='width:45px;'>数量</td><td>大包装</td><td>件数</td><td>生产日期</td><td>批次</td><td>保质期</td></tr></thead><tbody>" +
    storeFollowList.map(row => {
      return `<tr>
            <td>${row.itemCode}</td>
            <td>${row.barCode}</td>
            <td>${row.commodityName}</td>
            <td>${row.commodityStandard}</td>
            <td>${row.commodityCount}</td>
            <td>${row.commodityPacking}</td>
            <td>${row.procureNumber}</td>
            <td> </td>
            <td> </td>
            <td> </td>
          </tr>`
    }).join("") +
    "</tbody></table>" + "</body>";
  return style + tableStr;
}

function WaitFor() {
  c = c + 1;
  console.log("正等待(JOB代码是" + P_ID + ")打印结果：" + c + "秒");
  t = setTimeout(WaitFor, 1000);
  if (LODOP.GET_VALUE("PRINT_STATUS_OK", P_ID)) {
    clearTimeout(t);
    console.log("走的lodop插件方式：PRINT_STATUS_OK判断为打印成功");
    c = 0;
    alert("打印成功！");
  } if ((!LODOP.GET_VALUE("PRINT_STATUS_EXIST", P_ID)) && (c > 0)) {
    clearTimeout(t);
    console.log("走的lodop插件方式：判断该该任务已经不在队列，已成功或删除任务");
    c = 0;
    alert("打印任务不在队列，成功或删除");
  } else if (c > 50) {
    clearTimeout(t);
    console.log("打印超过50秒没捕获到成功状态！");
    c = 0;
    alert("打印超过50秒没捕获到成功状态！");
  };
};

function C_WaitFor() {
  c = c + 1;
  console.log("正等待(JOB代码是" + P_ID + ")打印结果：" + c + "秒");
  t = setTimeout(C_WaitFor, 1000);
  LODOP.On_Return_Remain = true;
  LODOP.On_Return = function (TaskID, Value) {
    console.log(Value, "@@@@@@@@@@@");
    console.log(TaskID, "TaskID");
    console.log(TaskID1, "TaskID1");
    console.log(TaskID2, "TaskID2");
    if (TaskID == TaskID1) {
      if (Value == 1) {
        alert(TaskID + " " + Value);
        clearTimeout(t);
        console.log("走的c-lodop方式：PRINT_STATUS_OK判断为打印成功");
        c = 0;
        alert("打印成功！");
      };
    } else
      if (TaskID == TaskID2) {
        if (Value == 0) {
          clearTimeout(t);
          console.log("走的c-lodop方式：判断该该任务已经不在队列，已成功或删除任务");
          c = 0;
          alert("打印任务不在队列，成功或删除");
        };
      };
  };
  TaskID1 = LODOP.GET_VALUE("PRINT_STATUS_OK", P_ID);
  TaskID2 = LODOP.GET_VALUE("PRINT_STATUS_EXIST", P_ID);
  if (c > 50) {
    clearTimeout(t);
    console.log("打印超过50秒没捕获到成功状态！");
    c = 0;
    alert("打印超过50秒没捕获到成功状态！");
  };
};